﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DC2010.Objects;
using DC2010.Effects;
using System.Drawing.Drawing2D;
using DC2010_Editor.Properties;
using DC2010.Tools;
using DC2010;
using DC2010.Conditions;

namespace DC2010_Editor.Formulars
{
    public partial class SimpleConditionList : Form
    {
        private List<SimpleCondition> lse = null;
        private EffectType et = EffectType.None;
        private SimpleCondition data { get; set; }

        public SimpleConditionList(List<SimpleCondition> lse)
        {
            InitializeComponent();

            this.lse = lse;
            this.et = et;

            C_IsItemInhands c1 = new C_IsItemInhands();
            c1.Info.Id = "C_ITEM_IN_HANDS";
            c1.Info.TemplateId = "C_ITEM_IN_HANDS";
            comboBox1.Items.Add(c1);

            C_IsItemInLeftHand c2 = new C_IsItemInLeftHand();
            c2.Info.Id = "C_ITEM_IN_LEFT_HAND";
            c2.Info.TemplateId = "C_ITEM_IN_LEFT_HAND";
            comboBox1.Items.Add(c2);

            C_IsPartyFacing c3 = new C_IsPartyFacing();
            c3.Info.Id = "C_IS_PARTY_FACING";
            c3.Info.TemplateId = "C_IS_PARTY_FACING";
            comboBox1.Items.Add(c3);
        }

        private void comboBox1_DrawItem(object sender, DrawItemEventArgs e)
        {
            Condition ef = (Condition)comboBox1.Items[e.Index];
            Font font = new Font("Tahoma", 7, FontStyle.Regular);
            Font fontBold = new Font("Tahoma", 8, FontStyle.Bold);
            Color clr1 = Color.White;
            Color clr2 = Color.White;

            clr1 = Color.Green;
            clr2 = Color.LightGreen;

            LinearGradientBrush lgb = new LinearGradientBrush(e.Bounds, clr1, clr2, 90f);

            e.Graphics.FillRectangle(lgb, e.Bounds);
            e.Graphics.DrawRectangle(new Pen(Color.FromArgb(200, 200, 250)), e.Bounds);

            Rectangle r = new Rectangle(e.Bounds.X, e.Bounds.Y, e.Bounds.Width, e.Bounds.Height);
            r.X = r.Width - 25;
            r.Width = 25;
            e.Graphics.DrawRectangle(new Pen(Color.FromArgb(200, 200, 250)), r);


            TextRenderer.DrawText(e.Graphics, ef.LongName, font, new Point(e.Bounds.X, e.Bounds.Y + 12), Color.Black);
            TextRenderer.DrawText(e.Graphics, ef.ShortName, fontBold, new Point(e.Bounds.X, e.Bounds.Y + 1), Color.Black);
        }

        private void comboBox1_MeasureItem(object sender, MeasureItemEventArgs e)
        {
            e.ItemHeight = 25;
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (comboBox1.SelectedItem != null)
            {
                Condition ef = (Condition)comboBox1.SelectedItem;

                if (ef != null) 
                    crystalButton1.Enabled = true;
                else
                    crystalButton1.Enabled = false;
            }
            else crystalButton1.Enabled = false;
        }

        private void crystalButton1_Click(object sender, EventArgs e)
        {
            if (comboBox1.SelectedItem != null)
            {
                Condition ef = (Condition)comboBox1.SelectedItem;

                SimpleCondition sef = new SimpleCondition();
                sef.Id = ef.Info.Id;
                sef.TemplateId = ef.Info.TemplateId;
                sef.Params = (ConditionParams)ef.Info.Params.Clone();

                lse.Add(sef);

                RefreshSefList();
            }
        }

        public void RefreshSefList()
        {
            if (imageListBox1.Items != null)
            {
                imageListBox1.Items.Clear();

                if (lse != null)
                {

                    foreach (SimpleCondition dp in lse)
                    {
                        Image i = Resources.lightning; //default

                        ImageListBoxItem itm = new ImageListBoxItem(string.Format("{0} ({1})", dp.Id,dp.Params.sParam1), i, dp);
                        imageListBox1.Items.Add(itm);
                    }
                }
            }

            RefreshParameters();
        }

        private void RefreshParameters()
        {
            if (data != null)
            {
                Condition eData = Root.I.IS.GetCondition(data.TemplateId);

                if (eData != null)
                {
                    label1.Text = eData.Info.Params.Param1_Desc;
                    label1.Visible = eData.Info.Params.Param1_Desc != null;
                    textBox1.Visible = eData.Info.Params.Param1_Desc != null;

                    label2.Text = eData.Info.Params.Param2_Desc;
                    label2.Visible = eData.Info.Params.Param2_Desc != null;
                    textBox2.Visible = eData.Info.Params.Param2_Desc != null;

                    label3.Text = eData.Info.Params.Param3_Desc;
                    label3.Visible = eData.Info.Params.Param3_Desc != null;
                    textBox3.Visible = eData.Info.Params.Param3_Desc != null;

                    label4.Text = eData.Info.Params.Param4_Desc;
                    label4.Visible = eData.Info.Params.Param4_Desc != null;
                    textBox4.Visible = eData.Info.Params.Param4_Desc != null;

                    label5.Text = eData.Info.Params.Param5_Desc;
                    label5.Visible = eData.Info.Params.Param5_Desc != null;
                    textBox5.Visible = eData.Info.Params.Param5_Desc != null;

                    label10.Text = eData.Info.Params.sParam1_Desc;
                    label10.Visible = eData.Info.Params.sParam1_Desc != null;
                    textBox10.Visible = eData.Info.Params.sParam1_Desc != null;

                    label9.Text = eData.Info.Params.sParam2_Desc;
                    label9.Visible = eData.Info.Params.sParam2_Desc != null;
                    textBox9.Visible = eData.Info.Params.sParam2_Desc != null;

                    label8.Text = eData.Info.Params.sParam3_Desc;
                    label8.Visible = eData.Info.Params.sParam3_Desc != null;
                    textBox8.Visible = eData.Info.Params.sParam3_Desc != null;

                    label7.Text = eData.Info.Params.sParam4_Desc;
                    label7.Visible = eData.Info.Params.sParam4_Desc != null;
                    textBox7.Visible = eData.Info.Params.sParam4_Desc != null;

                    label6.Text = eData.Info.Params.sParam5_Desc;
                    label6.Visible = eData.Info.Params.sParam5_Desc != null;
                    textBox6.Visible = eData.Info.Params.sParam5_Desc != null;

                }
            }
        }

        private void SimpleEffectList_Load(object sender, EventArgs e)
        {
            RefreshSefList();
        }

        private void SetControls()
        { 
            // ?? data.Id
            if (data.Params != null)
            {
                textBox1.Text = data.Params.Param1.ToString();
                textBox2.Text = data.Params.Param2.ToString();
                textBox3.Text = data.Params.Param3.ToString();
                textBox4.Text = data.Params.Param4.ToString();
                textBox5.Text = data.Params.Param5.ToString();

                textBox10.Text = data.Params.sParam1;
                textBox9.Text = data.Params.sParam2;
                textBox8.Text = data.Params.sParam3;
                textBox7.Text = data.Params.sParam4;
                textBox6.Text = data.Params.sParam5;

                seTextBox1.SetRelatedObject(data.Params.trueEffects, EffectType.Pump);
                seTextBox2.SetRelatedObject(data.Params.falseEffects, EffectType.Pump);

            }
        }

        private void GetControls()
        {
            // ?? data.Id
            data.Params.Param1 = Int32.Parse(textBox1.Text);
            data.Params.Param2 = Int32.Parse(textBox2.Text);
            data.Params.Param3 = Int32.Parse(textBox3.Text);
            data.Params.Param4 = Int32.Parse(textBox4.Text);
            data.Params.Param5 = Int32.Parse(textBox5.Text);

            data.Params.sParam1 = textBox10.Text;
            data.Params.sParam2 = textBox9.Text;
            data.Params.sParam3 = textBox8.Text;
            data.Params.sParam4 = textBox7.Text;
            data.Params.sParam5 = textBox6.Text;
        }

        private void imageListBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (imageListBox1.SelectedItem != null)
            {
                if (imageListBox1.SelectedItem is ImageListBoxItem)
                {
                    ImageListBoxItem we = (ImageListBoxItem)imageListBox1.SelectedItem;
                    data = (SimpleCondition)we.Tag;

                    SetControls();
                    RefreshParameters();
                }
            }
        }

        private void crystalButton3_Click(object sender, EventArgs e)
        {
            GetControls();
        }

        private void crystalButton2_Click(object sender, EventArgs e)
        {
            if (imageListBox1.SelectedItem != null)
            {
                if (imageListBox1.SelectedItem is ImageListBoxItem)
                {
                    ImageListBoxItem we = (ImageListBoxItem)imageListBox1.SelectedItem;
                    SimpleCondition sef = (SimpleCondition)we.Tag; ;

                    lse.Remove(sef);
                }
            }

            RefreshSefList();
        }
    }
}
